import QtQuick 1.0
import com.meego 1.0
import "Component"
import "CommomUtil.js" as Util
import "const.js" as Const
import "database.js" as Database
import "Forum.js" as ForumUtil

Page {
    id: forum_page

    tools: commonTools///TO DELETE!!

    orientationLock : PageOrientation.LockPortrait

    property string selectionForum: ""
    property string selectionOther: "hot_forum_btn"


TitleBar {
        id: forum_title
        anchors.top: parent.top; anchors.left: parent.left
        width: parent.width;
        titleName: "论坛"
}

    Rectangle{
    id: forum_type_rect
    width: parent.width; height: 200
    anchors.top: forum_title.bottom; anchors.left: parent.left
    color:"#ebeff9"
    ButtonColumn{
        id:bt_rect
        width: parent.width - 130
        height: 180
        anchors.centerIn: parent
        platformStyle: CustomButtonStyle{
            buttonWidth: 320
            buttonHeight: 80
        }
            //车型论坛
        Button {
                id:car_forum_btn
                width: area_forum_btn.width
                height: area_forum_btn.height
                NormalText {
                    text: "车型论坛"
                    anchors.left:parent.left
                    anchors.leftMargin: 15
                    anchors.verticalCenter: parent.verticalCenter
                    color: selectionForum=="car_forum_btn" ? Const.Color.white :"#000"
                    font.pointSize: Const.FontSize.normal
                    font.bold: true
                }
                onClicked: {
                    selectionForum = "car_forum_btn" ;
                    ForumUtil.setCurrentClubType("1");
                    mainPage.pageStack.push(Qt.resolvedUrl("ForumCardBrandList.qml"));
                }

            }

        //地区论坛
        Button {
            id:area_forum_btn
            width: 323
            height: 49
            NormalText {
                text: "地区论坛"
                anchors.left:parent.left
                anchors.leftMargin: 15
                anchors.verticalCenter: parent.verticalCenter
                color: selectionForum=="area_forum_btn" ? Const.Color.white :"#000"
                font.pointSize: Const.FontSize.normal
                font.bold: true
            }
            onClicked: {
                selectionForum = "area_forum_btn" ;
                ForumUtil.setCurrentClubType("2");
                mainPage.pageStack.push(Qt.resolvedUrl("ForumCityList.qml"));
            }

        }

        //主题论坛
        Button {
            id:topic_forum_btn
            width: area_forum_btn.width
            height: area_forum_btn.height
            NormalText {
                text: "主题论坛"
                anchors.left:parent.left
                anchors.leftMargin: 15
                anchors.verticalCenter: parent.verticalCenter
                color: selectionForum=="topic_forum_btn" ? Const.Color.white : "#000"
                font.pointSize: Const.FontSize.normal
                font.bold: true
            }
            onClicked: {
                selectionForum = "topic_forum_btn" ;
                ForumUtil.setCurrentClubType("3");
                mainPage.pageStack.push(Qt.resolvedUrl("ForumSubjectList.qml"));
            }
        }
    }

}

    Rectangle{
       id:forum_self_info_rect
       anchors.top: forum_type_rect.bottom
       color: Const.Color.PageBack
       width:parent.width
       height: 110
       ButtonRow {
       id:forum_self_bt
        anchors.centerIn: parent
        width: parent.width - 30; height: 60
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.verticalCenter: parent.verticalCenter
        platformStyle: ButtonStyle {
            inverted: false
            buttonWidth: 150
            buttonHeight: 60
            pressedTextColor: Const.Color.SelectBack
            checkedTextColor: Const.Color.PressBack
        }
        Button { text: "热门论坛";onClicked:forum_gridview.model=hotListDataModel;}
        Button { text: "最近浏览";onClicked:{
                refleshGetData(Const.FavoriteType.Recent);
                forum_gridview.model=localListDataModel;}}
        Button { text: "查看收藏";onClicked:{
                refleshGetData(Const.FavoriteType.Forum);
                forum_gridview.model=localListDataModel;
            }

        }
    }
    }

    Rectangle{
            id:forum_list_rect
            width:480
            height:305
            anchors.top: forum_self_info_rect.bottom
            color: Const.Color.PageBack
//            color: "red"
            Row{
                Item {
                    width: 15; height: 400
                }
                GridView {
                    id:forum_gridview
                     width: 470
                     height: 300
                     cellWidth: 160
                     cellHeight: 146
                     flow: GridView.TopToBottom

                     model: hotListDataModel

                     delegate:ForumPageClubListDelegate{}


                 }
            }

        }

ListModel{
    id:hotListDataModel
}

ListModel{
    id:localListDataModel
}

Component.onCompleted: {
     getHotForumList();
}

//获取热点论坛
function getHotForumList(){
           var doc = new XMLHttpRequest();
           doc.onreadystatechange = function() {
                if (doc.readyState == XMLHttpRequest.HEADERS_RECEIVED) {
                } else if (doc.readyState == XMLHttpRequest.DONE) {
                    var a = doc.responseXML.documentElement;

                    for(var i=0;i<a.childNodes.length;i++){
                        if("Clubs"==a.childNodes[i].nodeName){
                            var id="";
                            var name="";
                            var logo="";

                            for(var j=0;j<a.childNodes[i].childNodes.length;j++){

                                if("Club"==a.childNodes[i].childNodes[j].nodeName){
                                     for(var n=0;n<a.childNodes[i].childNodes[j].childNodes.length;n++){
                                        if("Id"==a.childNodes[i].childNodes[j].childNodes[n].nodeName){
                                            id=a.childNodes[i].childNodes[j].childNodes[n].childNodes[0].nodeValue;
                                        }else if("Name"==a.childNodes[i].childNodes[j].childNodes[n].nodeName){
                                            name=a.childNodes[i].childNodes[j].childNodes[n].childNodes[0].nodeValue;
                                        }else if("Logo"==a.childNodes[i].childNodes[j].childNodes[n].nodeName){
                                            logo=a.childNodes[i].childNodes[j].childNodes[n].childNodes[0].nodeValue;
                                        }else{
                                            continue;
                                        }
                                    }
                                   hotListDataModel.append({favId:id,favName:name,favLogo:logo});
                                }
                            }
                        }
                    }
                }
              }
           doc.open("GET", "http://sp.autohome.com.cn/Nokia/HotClub.ashx?type=1&PhoneWidth=480&count=9");
           doc.send();
}

  function refleshGetData(type){
       var data = Database.getAllFavorites(type);
       localListDataModel.clear();
        //从本地获取最近浏览的记录
      for(var i=0 ;i<data.length;i++){
        var favorite_item_tmp = data[i] ;
       //遍历加到列表中
        localListDataModel.append({
                 favId :favorite_item_tmp.favoriteId ,
                 favName : favorite_item_tmp.name ,
                 favLogo : favorite_item_tmp.logo
             }) ;
        }
      }

}
